package com.atguigu.mybatisplus.service.impl;

import com.atguigu.mybatisplus.mapper.RoleMapper;
import com.atguigu.mybatisplus.model.entity.Role;
import com.atguigu.mybatisplus.model.form.RoleForm;
import com.atguigu.mybatisplus.model.query.RolePageQuery;
import com.atguigu.mybatisplus.model.vo.DataRolePageVO;
import com.atguigu.mybatisplus.model.vo.RolePageVo;
import com.atguigu.mybatisplus.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 分页查询角色列表
     *
     * @param sysRolePageQuery
     * @return
     */
    @Override
    public DataRolePageVO getRolePage(RolePageQuery sysRolePageQuery) {
        // 初始化分页参数
        Long pageNum = sysRolePageQuery.getPageNum();
        Long pageSize = sysRolePageQuery.getPageSize();
        IPage<Role> page = new Page<>(pageNum, pageSize);
        // 构造查询条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        // 关键字模糊查询(角色名称、角色编码)
        String keywords = sysRolePageQuery.getKeywords();
        if (StringUtils.isNotBlank(keywords)) {
            queryWrapper
                    .like(Role::getName, keywords)
                    .or()
                    .like(Role::getCode, keywords);
        }
        // 根据创建时间范围查询
        LocalDateTime beginTime = sysRolePageQuery.getBeginTime();
        if (beginTime != null) {
            queryWrapper.ge(Role::getCreateTime, beginTime);
        }
        // 根据结束时间范围查询
        LocalDateTime endTime = sysRolePageQuery.getEndTime();
        if (endTime != null) {
            queryWrapper.le(Role::getCreateTime, endTime);
        }
        // 执行分页查询
        IPage<Role> roleIPage = roleMapper.selectPage(page, queryWrapper);
        // 实体对象转换为VO
        List<Role> records = roleIPage.getRecords();
        List<RolePageVo> rolePageVoList = new ArrayList<>();
        for (Role record : records) {
            RolePageVo rolePageVo = new RolePageVo();
            BeanUtils.copyProperties(record, rolePageVo);
            rolePageVoList.add(rolePageVo);
        }
        // 构建并返回最终分页结果
        DataRolePageVO dataRolePageVO = new DataRolePageVO();
        dataRolePageVO.setList(rolePageVoList);
        dataRolePageVO.setTotal(roleIPage.getTotal());
        return dataRolePageVO;
    }

    /**
     * 新增角色
     *
     * @param roleForm
     * @return
     */
    @Override
    public void saveRole(RoleForm roleForm) {
        Role role = new Role();
        BeanUtils.copyProperties(roleForm, role);
        roleMapper.insert(role);
    }

    /**
     * 更新角色
     *
     * @param roleForm
     */
    @Override
    public void updateRole(RoleForm roleForm) {
        Role role = new Role();
        BeanUtils.copyProperties(roleForm, role);
        roleMapper.updateById(role);
    }

    /**
     * 获取角色的菜单ID列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getMenuIds(Long roleId) {
        // 从角色菜单关联表查询菜单ID列表
        List<Integer> menuIds = roleMapper.selectMenuIdsByRoleId(roleId);
        return menuIds;
    }

    /**
     * 更新角色状态
     *
     * @param roleId
     * @param status
     */
    @Override
    public void updateStatus(Long roleId, Integer status) {
        Role role = new Role();
        role.setId(roleId);
        role.setStatus(status);
        roleMapper.updateById(role);
    }
}
